package com.jiuyou.clientController.product;

import com.alibaba.fastjson.JSONObject;
import com.jiuyou.clientController.base.ClientBaseController;
import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.PositioningUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.product.domain.StoreProduct;
import com.ruoyi.product.domain.StoreProductBack;
import com.ruoyi.product.service.IStoreProductBackService;
import com.ruoyi.product.service.IStoreProductService;
import com.ruoyi.searcWord.domain.SearchWord;
import com.ruoyi.searcWord.mapper.SearchWordMapper;
import com.ruoyi.searcWord.service.ISearchWordService;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品
 *
 * @author jiuyou
 */
@Api(tags = {"C端商品相关"})
@RestController
@RequestMapping("/client/product")
public class ProductClientController extends ClientBaseController
{
    private static final Logger log = LoggerFactory.getLogger(ProductClientController.class);
    @Autowired
    private IStoreProductService storeProductService;
    @Autowired
    private IStoreProductBackService backService;
    @Autowired
    private ISearchWordService searchWordService;
    @Autowired
    private ISysConfigService configService;

    @ApiOperation(value = "获取商品详情")
    @ApiImplicitParam(name = "id",value = "商品id",dataType = "Long")
    @PostMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        StoreProduct storeProduct=storeProductService.selectStoreProductById(id);
        if(null==storeProduct){
            return AjaxResult.error("参数错误");
        }
        JSONObject jso=new JSONObject();
        jso.put("storeId",storeProduct.getStoreId());
        jso.put("storeName",storeProduct.getStoreName());
        jso.put("productName",storeProduct.getProductName());
        jso.put("productImage",storeProduct.getProductImage());
        jso.put("itemDetail",storeProduct.getItemDetail());
        jso.put("infoId",storeProduct.getInfoId());
        jso.put("price",storeProduct.getPrice());
        jso.put("salesNum",storeProduct.getSalesNumShow());
        jso.put("state",storeProduct.getState());
        jso.put("fixedTimeState",storeProduct.getFixedTimeState());
        jso.put("useStartTime",DateUtils.dateTime(storeProduct.getUseStartTime()));
        jso.put("useEndTime", DateUtils.dateTime(storeProduct.getUseEndTime()));
        jso.put("disabledDate",storeProduct.getDisabledDate());
        jso.put("validDay",storeProduct.getValidDay());
        jso.put("appoinNum",storeProduct.getAppoinNum());
        jso.put("appoinUnit",storeProduct.getAppoinUnit());
        jso.put("peopleRight",storeProduct.getPeopleRight());
        jso.put("peopleNum",storeProduct.getPeopleNum());
        jso.put("ruleReminder",storeProduct.getRuleReminder());
        jso.put("reminder",storeProduct.getReminder());
        jso.put("service",storeProduct.getService());
        jso.put("couponsMaxNewUse",storeProduct.getCouponsMaxNewUse());
        jso.put("couponsMaxAll",storeProduct.getCouponsMaxAll());
        jso.put("storePrice",storeProduct.getStorePrice());
        jso.put("backId",storeProduct.getProductBackId());
        jso.put("classifyId",storeProduct.getClassifyId());


        return AjaxResult.success(jso);
    }

    /**
     * （新）查询商品列表
     * */
    @GetMapping("/listNew")
    public AjaxResult listNew(Long storeId,  Long classifyId) {
        StoreProduct storeProductParam=new StoreProduct();
//        if(StringUtils.isNotEmpty(searchValue)){
//            storeProductParam.getParams().put("searchValue",searchValue);
//            SearchWord searchWord = new SearchWord();
//            searchWord.setSearchValue(searchValue);
//            searchWordService.insertSearchWord(searchWord);
//        }
        storeProductParam.setState(DicConstants.SYS_Y.getCode());
        storeProductParam.setStoreId(storeId);
        storeProductParam.setClassifyId(classifyId);
        List<StoreProduct> list = storeProductService.selectStoreProductJointClientList(storeProductParam);
        TableDataInfo tableDataInfo=getDataTable(list);
        List<JSONObject> result=new ArrayList<JSONObject>();
        for (StoreProduct storeProduct:list) {
            JSONObject jso=new JSONObject();
            jso.put("id",storeProduct.getId());
            jso.put("storeId",storeProduct.getStoreId());
            jso.put("storeName",storeProduct.getStoreName());
            jso.put("productName",storeProduct.getProductName());
            jso.put("productImage",storeProduct.getProductImage());
            jso.put("price",storeProduct.getPrice());
            jso.put("storePrice",storeProduct.getStorePrice());
            jso.put("salesNum",storeProduct.getSalesNumShow());
            jso.put("couponsMaxNewUse",storeProduct.getCouponsMaxNewUse());
            jso.put("couponsMaxAll",storeProduct.getCouponsMaxAll());
            jso.put("backId",storeProduct.getProductBackId());
            jso.put("isVip",storeProduct.getIsVip());
            jso.put("vipPrice",storeProduct.getVipPrice());
            result.add(jso);
        }
        tableDataInfo.setRows(result);
        return AjaxResult.success(tableDataInfo);
    }
    /**
     * 查询商品列表
     */
    @ApiOperation(value = "查询商品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前第几页",required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize",value = "每页几条数据",required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "storeId",value = "门店id",dataType = "Long"),
            @ApiImplicitParam(name = "latitude",value = "纬度，经度纬度用来根据定位显示附近商品",required = true,dataType = "String"),
            @ApiImplicitParam(name = "longitude",value = "经度",required = true,dataType = "String"),
            @ApiImplicitParam(name = "newUseFlag",value = "是否新人专享是传Y否传N",dataType = "String"),
    })
    @GetMapping("/list")
    public AjaxResult list(Long storeId,String newUseFlag,String latitude, String longitude, String cityCode, Integer pageNum, Integer pageSize,Long classifyId,Long classifyParentId,String searchValue)
    {
        StoreProduct storeProductParam=new StoreProduct();
        if(DicConstants.SYS_Y.getCode().equals(newUseFlag)){
            //新人专享
            storeProductParam.getParams().put("newUseFlag",newUseFlag);
        }

        if(StringUtils.isNotEmpty(searchValue)){
            storeProductParam.getParams().put("searchValue",searchValue);
            SearchWord searchWord = new SearchWord();
            searchWord.setSearchValue(searchValue);
            searchWordService.insertSearchWord(searchWord);
        }
        storeProductParam.setState(DicConstants.SYS_Y.getCode());
        storeProductParam.setStoreId(storeId);
        storeProductParam.setClassifyId(classifyId);
        storeProductParam.setLatitude(latitude);
        storeProductParam.setLongitude(longitude);
        if( null != classifyParentId && 0 != classifyParentId){
            storeProductParam.setClassifyParentId(classifyParentId);
        }
        storeProductParam.setCityCode(cityCode);
        startPage();
        List<StoreProduct> list = storeProductService.selectStoreProductClientList(storeProductParam);
        TableDataInfo tableDataInfo=getDataTable(list);
        List<JSONObject> result=new ArrayList<JSONObject>();
        for (StoreProduct storeProduct:list) {
            JSONObject jso=new JSONObject();
            jso.put("id",storeProduct.getId());
            jso.put("storeId",storeProduct.getStoreId());
            jso.put("storeName",storeProduct.getStoreName());
            jso.put("productName",storeProduct.getProductName());
            jso.put("productImage",storeProduct.getProductImage());
            jso.put("price",storeProduct.getPrice());
            jso.put("storePrice",storeProduct.getStorePrice());
            jso.put("salesNum",storeProduct.getSalesNumShow());
            jso.put("couponsMaxNewUse",storeProduct.getCouponsMaxNewUse());
            jso.put("couponsMaxAll",storeProduct.getCouponsMaxAll());
            jso.put("backId",storeProduct.getProductBackId());
            jso.put("isVip",storeProduct.getIsVip());
            jso.put("vipPrice",storeProduct.getVipPrice());
            if(StringUtils.isNotEmpty(longitude)){
                jso.put("range", PositioningUtils.getDistance(Double.parseDouble(latitude),Double.parseDouble(longitude),Double.parseDouble(storeProduct.getLatitude()),Double.parseDouble(storeProduct.getLongitude())));
            }

            result.add(jso);
        }
        tableDataInfo.setRows(result);
        return AjaxResult.success(tableDataInfo);
    }
    @PostMapping(value = "/checkCan/{id}")
    public boolean checkCan(@PathVariable("id") Long id)
    {
        StoreProductBack storeProduct=backService.selectStoreProductBackById(id);
        if(null==storeProduct || DicConstants.SYS_N.equals(storeProduct.getState())){
           return false;
        }
        return true;
    }

    @ApiOperation(value = "获取商品详情")
    @ApiImplicitParam(name = "id",value = "商品id",dataType = "Long")
    @PostMapping(value = "/getTemp/{id}")
    public AjaxResult getTemp(@PathVariable("id") Long id)
    {
        StoreProductBack storeProductBack=backService.selectStoreProductBackById(id);
        if(null==storeProductBack){
            return AjaxResult.error("参数错误");
        }
        JSONObject jso=new JSONObject();
        jso.put("storeId",storeProductBack.getStoreId());
        jso.put("storeName",storeProductBack.getStoreName());
        jso.put("productName",storeProductBack.getProductName());
        jso.put("productImage",storeProductBack.getProductImage());
        jso.put("itemDetail",storeProductBack.getItemDetail());
        jso.put("price",storeProductBack.getPrice());
        jso.put("salesNum",storeProductBack.getSalesNum());
        jso.put("state",storeProductBack.getState());
        jso.put("fixedTimeState",storeProductBack.getFixedTimeState());
        jso.put("useStartTime",storeProductBack.getUseStartTime());
        jso.put("useEndTime",storeProductBack.getUseEndTime());
        jso.put("disabledDate",storeProductBack.getDisabledDate());
        jso.put("validDay",storeProductBack.getValidDay());
        jso.put("appoinNum",storeProductBack.getAppoinNum());
        jso.put("appoinUnit",storeProductBack.getAppoinUnit());
        jso.put("peopleRight",storeProductBack.getPeopleRight());
        jso.put("peopleNum",storeProductBack.getPeopleNum());
        jso.put("ruleReminder",storeProductBack.getRuleReminder());
        jso.put("reminder",storeProductBack.getReminder());
        jso.put("service",storeProductBack.getService());
        jso.put("productId",storeProductBack.getProductId());
        jso.put("storePrice",storeProductBack.getStorePrice());
        jso.put("classifyId",storeProductBack.getClassifyId());

        jso.put("fictiNum",storeProductBack.getFictiNum());
        return AjaxResult.success(jso);
    }
}
